﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SacNetUG.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SacNetUG.Models.RSVP>>" %>
<%@ Import Namespace="SacNetUG.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    RSVPs
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%
    var name = "";
    var date = "";
    var eventId = 0;
    if(ViewData["nextEvent"] != null)
    {
        var evt = (Event) ViewData["nextEvent"];
        name = " for<br />" + evt.Name + " ";
        date = evt.StartDate.ToShortDateString();
        eventId = evt.EventId;
    }
     %>
    <h2>RSVPs<%=name %><%: date %></h2>
    <%: ViewData["Unavailable"] %>
    <table>
        <tr>
            <th>
                First
            </th>
            <th>
                First
            </th>
            <th>
                Email
            </th>
            <th>
                Attending
            </th>
            <th>
                Attended
            </th>
        </tr>

    <% foreach (var item in Model) { 
           var chk = (item.Attended.HasValue && item.Attended.Value) ? "checked='checked'" : "";
           
           %>
    
        <tr>
            <td>
                <%= Html.Encode(item.User.FirstName) %>
            </td>
            <td>
                <%= Html.Encode(item.User.LastName) %>
            </td>
            <td>
                <%= Html.Encode(item.User.EmailAddress) %>
            </td>
            <td>
                <%= Html.Encode(item.WillAttend) %>
            </td>
            <td style="text-align:center;"><input type="checkbox" class="attended" userid="<%: item.UserId %>" <%:chk%> /></td>
        </tr>
    
    <% } %>

    </table>
    <input type="button" value="Add User" id="add" />
    <div id="userSearch" style="display:none;"> 
        First Name: <input type="text" id="name" /><br />
        Last Name: <input type="text" id="last" /><br />
        EMail: <input type="text" id="email" /><br />
        <input type="button" value="Find" id="find" />&nbsp;
    <input type="button" value="Create" id="create" />
        <div id="results"></div>
    </div> 
    <script type="text/javascript">
    var eventId = <%=eventId%>;
        $(function () {
            $(document).ajaxStop($.unblockUI); 
            $("#userSearch").dialog({ autoOpen: false, title: 'Please enter name or email:', width:600 });



            $('.attended').click(
            //send the user id attribute to the update attending function
                function () {
                    $.blockUI(); 
                    var userId = $(this).attr('userid');

                    markAttending(userId, $(this).attr('checked'));
                }
            );


            $('#add').click(
                function () {
                    $('#userSearch').dialog('open');
                    $('#name').focus();
                }
            );

            $('#find').click(
                function () {
                    var name = $('#name').val();
                    var last = $('#last').val();
                    var email = $('#email').val();

                    if(name.length == 0 & last.length == 0 & email.length == 0)
                    {
                        alert('First, Last and EMail required');
                        Return;
                    }

                    $.blockUI(); 

                   findUser($('#name').val(), $('#last').val(), $('#email').val());
                }
            );

            $('#create').click(
                function () {
                    var name = $('#name').val();
                    var last = $('#last').val();
                    var email = $('#email').val();

                    if(name.length == 0 | last.length == 0 | email.length == 0)
                    {
                        alert('First, Last and EMail required');
                        Return;
                    }

                    $.blockUI(); 

                   createUser(name, last, email);
                    $('#userSearch').dialog('close');
                    $('#name').val('');
                    $('#results').html('');
                }
            );
        });

        function selectUser(userId){
            $.blockUI(); 
            addUser(userId);
            $('#userSearch').dialog('close');
            $('#name').val('');
            $('#results').html('');
        }

        function createUser(name, last, email){
            $.ajax({
                type: "POST",
                cache: false,
                url: '<%=Url.Action("CreateUser", "Event")%>',
                data: { name: name, last: last, email: email },
                success: function (data) {
                    if(data.length > 0)
                        alert(data);
                    else
                        location.reload();
                },
                error: function (request, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }

        function addUser(userId){
            $.ajax({
                type: "POST",
                cache: false,
                url: '<%=Url.Action("AddUserById", "Event")%>',
                data: { userId: userId },
                success: function (data) {
                    if(data.length > 0)
                        alert(data);
                    else
                        location.reload();
                },
                error: function (request, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }

        function findUser(name, last, email){
            $.ajax({
                type: "POST",
                cache: false,
                url: '<%=Url.Action("FindUser", "Event")%>',
                data: { name: name, last: last, email: email },
                success: function (data) {
                    $('#results').html(data);
                },
                error: function (request, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }

        function markAttending(userId, attending){
            $.ajax({
                type: "POST",
                cache: false,
                url: '<%=Url.Action("Attended", "Event")%>',
                data: { userId: userId, attending: attending },
                success: function (data) {
                    if(data.length > 0)
                        alert(data);
                },
                error: function (request, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }
    </script>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server">
</asp:Content>

